<template>
  <div class="container">
    <el-card class="property-container">
      <!-- 操作区域 -->
      <el-row class="card-header" type="flex" justify="space-between">
        <el-col :span="3">
          <el-button type="primary" @click="addProperty">添加房产</el-button>
        </el-col>
        <el-col :span="17">
          <el-form class="demo-form-inline" :inline="true" width="60px">
            <el-form-item label="全部分类">
              <el-select v-model="PropertiesForm.category" clearable placeholder="全部分类" style="width:120px" @clear="clearable">
                <el-option v-for="item in categoryList" :key="item.id" :label="item.title" :value="item.id" />
              </el-select>
            </el-form-item>
            <el-form-item label="全部经纪">
              <el-select v-model="PropertiesForm.agent" clearable placeholder="全部经纪" @clear="clearable">
                <el-option v-for="item in agentsList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-input v-model="PropertiesForm.title_contains" placeholder="请输入关键字" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="small" @click="searchProperty">筛选</el-button><el-button type="success" size="small" @click="searchAll">全部</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-divider />
      <!-- 表格 -->
      <el-table
        v-if="PropertiesList[0]"
        :data="PropertiesList"
        style="width: 100%"
        border=""
      >
        <el-table-column
          prop="image"
          label="封面"
          width="120"
        >
          <template v-slot="scope">
            <img :src="scope.row.image" alt="" style="width:120px;height:100px;object-fit:cover;">
          </template>
        </el-table-column>
        <el-table-column
          prop="title"
          label="标题"
        />
        <el-table-column
          prop="address"
          label="地址"
        />
        <el-table-column
          prop="tags"
          label="标签"
        >
          <template v-slot="scope">
            <el-tag v-for="(item,index) in scope.row.tags" :key="index" type="success">{{ item }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          label="地址"
        />
        <el-table-column
          prop="category.title"
          label="分类"
        />
        <el-table-column
          prop="agent.name"
          label="经纪"
        />
        <el-table-column
          prop="price"
          label="价格"
        />
        <el-table-column
          prop="desc"
          label="介绍"
          width=""
        />
        <el-table-column
          prop="photos"
          label="图片"
          width=""
        >
          <template v-slot="scope">
            <img v-for="(item,index) in scope.row.photos" :key="index" style="width:50px;height:50px;object-fit:cover;margin-left:10px;" :src="item">
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="150"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row
        type="flex"
        justify="center"
        align="middle"
        style="height: 60px"
      >
        <!-- 分页组件 -->
        <el-pagination v-if="PropertiesList[0]" layout="prev,pager,next" :total="total" :page-size="getPage._limit" :current-page="currentPage" @current-change="currentChange" />
      </el-row>
    </el-card>
    <!-- 组件 -->
    <AddProperty ref="editProperty" :is-show-dialog="isShowDialog" :category-list="categoryList" :agents-list="agentsList" :get-page="getPage" /></div>
</template>

<script>
import { getPropertiesList, getPropertiesCount, delProperties } from '@/api/property'
import AddProperty from './components/add-property.vue'
import { getCategories } from '@/api/categories'
import { getAgentsList } from '@/api/agents'
export default {
  components: {
    AddProperty
  },
  data() {
    return {
      PropertiesForm: {
        title_contains: '',
        agent: '',
        category: ''
      },
      getPage: {
        _limit: 3,
        _start: 0
      },
      form: {

      },
      currentPage: 1,
      newPropertiesList: [],
      PropertiesList: [],
      total: 0,
      isShowDialog: false,
      categoryList: [],
      agentsList: []
    }
  },
  async created() {
    // 获取分类
    const data = await getCategories()
    this.categoryList = data
    const Agents = await getAgentsList()
    this.agentsList = Agents
    // 获取房产列表
    this.getPropertiesList({ ...this.getPage, ...this.form })
    // 获取房产数量
    this.getPropertiesCount({ ...this.getPage, ...this.form })
  },
  methods: {
    // 清空选择框选项
    clearable() {
      this.form = {}
    },
    currentChange(page) {
      this.currentPage = page
      console.log(175, this.currentPage)
      // 计算从索引开始
      this.getPage._start = this.getPage._limit * (this.currentPage - 1)
      // 获取房产列表
      this.getPropertiesList({ ...this.getPage, ...this.form })
    },
    // 添加房产
    addProperty() {
      this.isShowDialog = true
    },
    // 编辑房产
    handleEdit(data) {
      this.nodeData = data
      // console.log(await this.$refs.editProperty.getPropertiesListById())
      this.$refs.editProperty.getProperties(data)
      this.isShowDialog = true
    },
    async getPropertiesList(params) {
      this.PropertiesList = await getPropertiesList(params)
    },
    async getPropertiesCount(params) {
      this.total = await getPropertiesCount(params)
    },
    // 删除房产
    async handleDelete(id) {
      await this.$confirm('此操作删除该数据, 是否继续?')
      if (this.PropertiesList.length === 1) {
        this.currentPage--
        this.currentChange(this.currentPage)
      }
      await delProperties(id)
      this.$message({
        message: '删除成功',
        type: 'success'
      })
      this.getPropertiesCount({ ...this.getPage, ...this.form })
      this.getPropertiesList({ ...this.getPage, ...this.form })
    },
    // 查询房产
    searchProperty() {
      console.log(218, this.PropertiesForm)
      // this.PropertiesForm.agent
      if (this.PropertiesForm.agent) {
        this.form.agent = this.PropertiesForm.agent
      }
      if (this.PropertiesForm.category) {
        this.form.category = this.PropertiesForm.category
      }
      if (this.PropertiesForm.title_contains) {
        this.form.title_contains = this.PropertiesForm.title_contains
      }
      this.getPage._start = 0
      this.currentPage = 1
      this.getPropertiesList({ ...this.form, ...this.getPage })
      this.getPropertiesCount({ ...this.form, ...this.getPage })
    },
    // 全部房产
    searchAll() {
      this.form = {}
      this.getPropertiesList(this.getPage)
      this.getPropertiesCount(this.getPage)
    }
  }
}
</script>

<style lang="scss" scoped>

.container {
  padding: 20px;
}
.main-container {
    padding: 20px;
    width: 100%;
    margin-left: 0;
    background-color: #666;
    .property-container {
      padding: 20px;
    }
}
.card-header {

}
.el-table .cell, .el-table--border td:first-child .cell, .el-table--border th:first-child .cell {
    padding: 10px !important;
}
</style>
